// index.js
Page({
  data: {
    messages: [],
    inputText: ''
  },

  onInput(e) {
    this.setData({ inputText: e.detail.value });
  },

  send() {
    const question = this.data.inputText.trim();
    if (!question) return;

    // 添加用户消息
    const newMessages = [...this.data.messages, { role: 'user', content: question }];
    this.setData({ messages: newMessages, inputText: '' });

    // 发送请求到扣子 AI
    wx.request({
      url: 'https://api.coze.cn/v3/chat',
      method: 'POST',
      header: {
        'Authorization': 'Bearer pat_3b0zwqc04km4BMBfIaY53Q9J3ZGIMYZcBCnhJz7ivurhtsuYN5bI90oyDfXOofQ7',
        'Content-Type': 'application/json'
      },
      data: {
        bot_id: "1174232090994",
        user_i: "2104795381",

        stream: false,
        auto_save_history: true,
        additional_messages: [
          {
            role: "user",
            content: question,
            content_type: "text"
          }
        ]


      },
      success: (res) => {
        const answer = res.data?.response || '暂时无法回答，请稍后再试。';
        this.setData({
          messages: [
            ...this.data.messages,
            { role: 'user', content: question },
            { role: 'assistant', content: answer }
          ]
        });
      },
      fail: () => {
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });
  }
});